<?php
ignore_user_abort(true);
set_time_limit(0);
if (isset($_POST['hook']) && $data = json_decode($_POST['hook'], true))
{
    if ($data['password'] === 'flzxsqcy5yhljt')
    {
        switch ($data['hook_name'])
        {
            case 'push_hooks':
                $cmd = 'git pull';
                break;
            default:
                $cmd = '';
                break;
        }
        if ($cmd)
        {
            $out = shell_exec($cmd);
        }
        else
        {
            $out = $_POST['hook'];
        }
        file_put_contents('gitpull.log', date('Y-m-d H:i:s')."\n".$cmd."\n".$out."\n", FILE_APPEND);
    }
    else
    {
        file_put_contents('gitpull.log', date('Y-m-d H:i:s')."\n"."密码错误\n".var_export($_POST, true)."\n", FILE_APPEND);
    }
}
else
{
    $out = shell_exec('git pull');
    echo '<pre>';
    print_r($out);
    echo '</pre>';
    file_put_contents('gitpull.log', date('Y-m-d H:i:s')."\n".var_export([$_GET, $_POST, $_SERVER], true)."\n", FILE_APPEND);
}
/*'{"hook":"{\"password\":\"flzxsqcy5yhljt\",\"hook_name\":\"push_hooks\",\"push_data\":{\"before\":\"0000000000000000000000000000000000000000\",\"after\":\"ec7159240a346fa5988913aa3057b902a4acb126\",\"ref\":\"refs\/heads\/master\",\"user_name\":\"k-k\",\"user\":{\"id\":537525,\"email\":\"2727586680@qq.com\",\"name\":\"k-k\",\"user\":\"k-k\",\"url\":\"https:\/\/git.oschina.net\/k-k\",\"time\":\"2017-09-03T15:47:40+08:00\"},\"repository\":{\"name\":\"aoyunhui\",\"url\":\"https:\/\/git.oschina.net\/k-k\/aoyunhui.git\",\"description\":\"\\u6fb3\\u8fd0\\u6c47\",\"homepage\":\"https:\/\/git.oschina.net\/k-k\/aoyunhui\"},\"commits\":[{\"id\":\"ec7159240a346fa5988913aa3057b902a4acb126\",\"message\":\"A Test For WebHooks\",\"timestamp\":\"2015-11-06T13:21:07+08:00\",\"url\":\"https:\/\/git.oschina.net\/k-k\/aoyunhui\/commit\/ec7159240a346fa5988913aa3057b902a4acb126\",\"author\":{\"name\":\"k-k\",\"email\":\"2727586680@qq.com\",\"time\":\"2015-11-06T13:21:07+08:00\"}}],\"total_commits_count\":2,\"commits_more_than_ten\":false}}"}';*/
